import axios, { AxiosRequestConfig, AxiosInstance, AxiosResponse } from "axios"
import { getCookieSync } from "../utils/cookie"

const options: AxiosRequestConfig = {
    baseURL: "http://localhost:5200",
    timeout: 5000,
}
const server: AxiosInstance = axios.create(options)


server.interceptors.request.use(((config: AxiosRequestConfig) => {
    let token = getCookieSync("token")
    config.headers.Authorization = token.replaceAll('"', '')
    return config;
}))

server.interceptors.response.use(
    (res: AxiosResponse<any>) => {
        if (res.status !== 200) {
            window.$message.error(res.data.message)
        }
        return res;
    },
    (err: any) => {
        window.$message.error("服务器异常！")
        throw new Error(err)
    })

export default server;